<?php

class Tourism_goods_image_model extends CI_Model
{
    private $table = 'tourism_goods_image';

    /*
     * 根据商品获取多图
     * */
    public function findByGoodId($goodsId)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('goods_id', $goodsId);
        $this->db->order_by('is_main','DESC');
        return $this->db->get();
    }

    /*
     * 插入图片
     * */
    public function insertImage($goods_id, $imageData)
    {

        $this->db->where('goods_id', $goods_id);
        $result = $this->db->get($this->table);
        $data = array(
            'goods_id' => $goods_id,
            'images' => trim($imageData['file_name']),
        );
        if ($result->num_rows() == 0) {
            $data['is_main'] = 1;
        }

        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    /*
     * 插入图片
     * */
    public function insertImageLine($goods_id, $imageData)
    {
        $data = array(
            'goods_id' => $goods_id,
            'images' => trim($imageData['file_name']),
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    /*
    * 设置默认
    * */
    public function mainImage($id, $goods_id)
    {
        $this->db->where('goods_id', $goods_id);
        $this->db->update($this->table, array('is_main' => 0));
        $this->db->where('id', $id);
        return $this->db->update($this->table, array('is_main' => 1));
    }

}